<template>
    <el-container class="home-container">
        <!-- 头部区域 -->
        <el-header>
            <div>
                <img src="../assets/ironman.jpg" alt="">
                <span>电商后台管理系统</span>
            </div>
            <el-button type="info" @click="logout">退出</el-button>
        </el-header>
        <!-- 页面主体区 -->
        <el-container>
            <!-- 侧边栏 -->
            <el-aside :width="isCollapse ? '64px':'200px'">
                <div class="toggle-button" @click="toggleCollpase">|||</div>
                <!-- 侧边栏菜单区域 -->
                <el-menu background-color="#333744" text-color="#fff" active-text-color="#449eff" :unique-opened="true"
                    :collapse="isCollapse" :collapse-transition="false" :router="true" :default-active="activePath">
                    <!-- 一级菜单 -->
                    <el-submenu :index="index+1 + ''" v-for="(item,index) in menulist" :key="item.id">
                        <!-- 模板区域 -->
                        <template slot="title">
                            <i :class="iconsObj[item.id]"></i>
                            <span>{{item.authName}}</span>
                        </template>
                        <!-- 二级菜单 -->
                        <el-menu-item :index="'/' + subitem.path + ''" v-for="subitem in item.children"
                            :key="subitem.id" @click="saveState('/' + subitem.path)">
                            <template slot="title">
                                <i class="el-icon-menu"></i>
                                <span>{{subitem.authName}}</span>
                            </template>
                        </el-menu-item>
                    </el-submenu>
                </el-menu>
            </el-aside>
            <!-- 右侧main -->
            <el-container>
                <el-main>
                    <!-- welcome -->
                    <router-view></router-view>
                </el-main>
            </el-container>
        </el-container>
    </el-container>
</template>

<script>
    export default {
        data() {
            return {
                menulist: [], // 权限控制的数据
                iconsObj: {
                    '125': 'iconfont icon-user',
                    '103': 'iconfont icon-tijikongjian',
                    '101': 'iconfont icon-shangpin',
                    '102': 'iconfont icon-danju',
                    '145': 'iconfont icon-baobiao'
                }, // 权限自定义图标(比较憨批的操作=>根据id去填写死的类名,主要是后台没写这个东西)
                isCollapse: false, //表单是否只打开一个其他关闭
                activePath: '' //被激活的连接地址
            }
        },
        created() {
            this.getMenuList();
            this.activePath = window.sessionStorage.getItem('active');
        },
        methods: {
            // 退出登录
            logout() {
                window.sessionStorage.clear();
                this.$router.push('/login');
            },
            // 获取权限管理所有菜单
            async getMenuList() {
                const { data: res } = await this.$http.get('menus')
                if (res.meta.status == 200) {
                    this.menulist = res.data;
                } else {
                    this.$message.error(res.meta.msg)
                }
            },
            //点击按钮切换权限菜单折叠展开
            toggleCollpase() {
                this.isCollapse = !this.isCollapse
            },
            // 保存连接的active状态
            saveState(activePath){
                window.sessionStorage.setItem('active',activePath);
                this.activePath = activePath;
            }
        }
    }
</script>

<style lang="scss" scoped>
    .el-header {
        height: 80px !important;
        background: #373d41;
        display: flex;
        justify-content: space-between;
        padding-left: 0px;
        align-items: center;
        color: #fff;
        font-size: 20px;

        >div {
            height: 80px;
            display: flex;
            align-items: center;

            span {
                margin-left: 15px;
            }
        }

        img {
            height: 100%;
            border-radius: 50%;
        }
    }

    .el-aside {
        background: #333744;

        .el-menu {
            border-right: none;
        }
    }

    .el-main {
        background: #eaedf1;
    }

    .home-container {
        height: 100%;

        .iconfont {
            margin-right: 10px;
        }

        .toggle-button {
            width: 100%;
            margin: 0 auto;
            background: #4a5064;
            font-size: 10px;
            line-height: 24px;
            color: #fff;
            text-align: center;
            letter-spacing: 0.2em;
            cursor: pointer;
        }
    }
</style>